/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package amd.database;

import java.util.ArrayList;

/**
 *
 * @author Latifah Dwi Nurachma
 */
public class DbDocument {

    public static ArrayList<String> getAllIdDocuments(ConnectDB conn) throws QueryNoResultException {
        String sql = "SELECT id_doc FROM document ORDER BY id_doc ASC";
        ArrayList<ArrayList<Object>> obj = conn.getAllDatas(sql);
        ArrayList<String> listIdDoc = new ArrayList<String>();
        int i=0;
        while(i<obj.get(0).size()){
            listIdDoc.add(obj.get(0).get(i).toString());
            i++;
        }
        return listIdDoc;
    }
    public static ArrayList<String> getAllIdDocs(ConnectDB conn) throws QueryNoResultException {
        String sql = "SELECT id_doc FROM document ORDER BY id_doc ASC";
//        ArrayList<ArrayList<Object>> obj = conn.getAllDatas(sql);
        ArrayList<Object> obj = conn.getAllIdDatas(sql);
        ArrayList<String> listIdDoc = new ArrayList<String>();
        System.out.println("Masuk getAllIdDocs");
        int i=0;
        while(i<obj.size()){
            listIdDoc.add(obj.get(i).toString());
            System.out.println(listIdDoc.get(i));
            i++;
        }
        return listIdDoc;
    }

    public static ArrayList<String> getDataDocument(ConnectDB conn, String idDoc) throws QueryNoResultException{
        String sql = "SELECT id_doc, title, abstract, location FROM document where id_doc = \'"+ idDoc +"\'";
        ArrayList<ArrayList<Object>> obj = conn.getAllDatas(sql);
        ArrayList<String> dataDocument = new ArrayList<String>();
        int i=0;
        while(i<obj.size()){
            ArrayList<Object> ob = obj.get(i);
            for(int j=0; j<ob.size(); j++)
              dataDocument.add(ob.get(j).toString());
//            dataDocument.add(obj.get(j).toString().replace('/', '\\'));
            i++;
        }
        return dataDocument;

    }

    public static void insert(ConnectDB conn, String idDoc){
        String sql = "INSERT INTO document (id_doc) VALUES (\'"+idDoc+"\')" /*.... values....*/;
        conn.createStatement(sql);
    }

    public static void insert(ConnectDB conn, String idDoc, String title, String abstractDoc, String location, int pjg){
        System.out.println("\nMasuk DbDocument.insert <======");
        System.out.println("\npanjang Document="+pjg);
        String sql ="INSERT INTO document (id_doc, title, abstract, location, panjang) VALUES (\'"+ idDoc
                +"\', \'" + title+ "\', \'"+abstractDoc+"\', \'"+ location+"\'," + pjg+")";
        System.out.println("panjang abstract="+abstractDoc.length());
        System.out.println("sql="+sql);

        conn.createStatement(sql);
    }

    public static String getIdDocument(ConnectDB conn, int index) throws QueryNoResultException {
        String sql = "SELECT id_doc FROM document ORDER BY id_doc ASC LIMIT "+ index +",1";
//        String idDoc = conn.getSingleData(sql, 1).toString();
        String idDoc = conn.getSingleData(sql, 1).toString();
        return idDoc;
    }

    public static int getNDocument(ConnectDB conn) {
        int nDoc=conn.countRows("document");
        return nDoc;
    }

    public static boolean isExist(ConnectDB conn, String abstractDoc) throws QueryNoResultException{
        String sql = "SELECT * FROM document WHERE abstract = \'" + abstractDoc + "\' ";
        return conn.isExistInDatabase(sql);
    }
    public static boolean isJudulExist(ConnectDB conn, String judulDoc) throws QueryNoResultException{
        String sql = "SELECT * FROM document WHERE title = \'" + judulDoc + "\' ";
        return conn.isExistInDatabase(sql);
    }

        public static boolean isPjgStringSamaExist(ConnectDB conn, int pstrgDoc) throws QueryNoResultException{
        String sql = "SELECT * FROM document WHERE panjang = " + pstrgDoc ;
        return conn.isExistInDatabase(sql);
    }

}
